/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.DAO;

import com.webaccess.retailws.Bean.MailBean;
import com.webaccess.retailws.db.DBConnection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 *
 * @author Windows
 */
public class MailDAO {

    DBConnection dbc = null;
    Connection conn = null;
    ResultSet rs = null;
    String result = null;
    StringBuffer myQuery = null;
    MailBean mailInfo = null;

    public MailDAO() {
        try {
            dbc = new DBConnection();
            //  conn = (Connection) dbc.getConnection();
            mailInfo = new MailBean();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public Long saveMailInfo(MailBean mailInfo) {
        Long mailId = 0L;
        try {
            if (conn == null || conn.isClosed()) {
                conn = (Connection) dbc.getConnection();
            }
            myQuery = new StringBuffer();

            myQuery.append("CALL sp_savemailinfo(NULL,'");
            myQuery.append(mailInfo.getToAddress());
            myQuery.append("','");
            myQuery.append(mailInfo.getFromAddress());
            myQuery.append("','");
            myQuery.append(mailInfo.getMsgSubject());
            myQuery.append("','");
            myQuery.append(mailInfo.getMsgContent());
            myQuery.append("','");
            myQuery.append(mailInfo.getMsgDateTime());
            myQuery.append("','");
            myQuery.append(mailInfo.getUserId());
            myQuery.append("','");
            myQuery.append(mailInfo.getPurpose());
            myQuery.append("')");
            rs = dbc.executeProcedure(conn, myQuery.toString());
            if (rs != null) {
                String rsValue = String.valueOf(rs);
                if (!rsValue.equalsIgnoreCase("Result set representing update count of 0")) {
                    while (rs.next()) {
                        mailId = rs.getLong("sp_MailId");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dbc.freeResultSet(rs);
                dbc.freeConnection(conn);
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return mailId;
        }
    }
}
